#include "lkhwindow.h"
#include "ui_lkhwindow.h"
#include <QDebug>
#include <QTextCodec>
#include <QTreeView>

LKHWindow::LKHWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LKHWindow)
{
    ui->setupUi(this);

    mp_work_window = new FormWorkWindow(this);
    //mp_work_window->setWindowFlags(Qt::Tool);//|Qt::WindowStaysOnTopHint);
    ui->layout_work->addWidget(mp_work_window,0,0,0,0);

    mp_dbms = new HousingDBMS(this);
    mp_models = new ModelContainer(this);
    mp_dbms->set_models(mp_models);

    mp_settings_dlg = new DlgSettings(this);
    mp_develop = new DevelopArea(mp_dbms, &m_hs,this);
    mp_about_dlg = new DlgAbout(this);

    mp_get_receipt = new FormGetReceipt(mp_settings_dlg, this);
    mp_get_receipt->set_dbms(mp_dbms);
    mp_get_receipt->hide();

    mp_town_editor = new FormTown(this);
    mp_town_editor->hide();
    mp_town_editor->set_dbms(mp_dbms);
    mp_town_editor->hide_sel_btn();

    mp_people_editor = new FormPeople(this);
    mp_people_editor->hide();
    mp_people_editor->set_dbms(mp_dbms);
    mp_people_editor->hide_sel_btn();

    mp_decmaster_editor = new FormDecMasters(this);
    mp_decmaster_editor->hide();
    mp_decmaster_editor->set_dbms(mp_dbms);
    mp_decmaster_editor->hide_sel_btn();

    mp_leases_editor = new FormLeases(this);
    mp_leases_editor->hide();
    mp_leases_editor->set_dbms(mp_dbms);
    mp_leases_editor->set_settings(mp_settings_dlg);

    mp_payments = new FormPayments(this);
    mp_payments->hide();
    mp_payments->set_dbms(mp_dbms);

    mp_reports = new FormReports(this);
    mp_reports->hide();
    mp_reports->set_dbms(mp_dbms);
    mp_reports->set_settings(mp_settings_dlg);

    connect(mp_dbms, SIGNAL(info_message(QString)), this, SLOT(get_msg(QString)));
    connect(mp_dbms, SIGNAL(error_message(QString)), this, SLOT(get_error_msg(QString)));
    connect(mp_dbms, SIGNAL(fast_message(QString)), this, SLOT(get_fast_msg(QString)));

    ui->qact_town->setEnabled(false);
    ui->qact_tenants->setEnabled(false);
    ui->qact_leases->setEnabled(false);
    ui->qact_receipt->setEnabled(false);
    ui->qact_decmasters->setEnabled(false);
    ui->qact_payments->setEnabled(false);
    ui->qact_reports->setEnabled(false);

    lakhMsg()<<RUSTR("СУБД учета найма муниципального жилья г. Лакинска. версия программы 1.1.1");

    if(mp_settings_dlg->is_auto_connect())
        on_qact_connect_activated();
    mp_get_receipt->set_doc_dir_path(mp_settings_dlg->get_doc_dirpath());

    ui->menuDevelop->hide();
    ui->menuDevelop->setTitle(QString());
    show_work_widget(mp_about_dlg, QString());

}

LKHWindow::~LKHWindow()
{
    delete ui;
}

void LKHWindow::show_work_widget(QWidget *pWidget, const QString & qstr_title)
{

    mp_work_window->set_title(qstr_title);
    mp_work_window->set_widget(pWidget);
    mp_work_window->show();
}

void LKHWindow::on_qact_edit_settings_activated()
{
    show_work_widget(mp_settings_dlg, RUSTR("Нстройки программы."));
}

void LKHWindow::on_qact_exit_activated()
{
    close();
}

void LKHWindow::on_qact_connect_activated()
{
    mp_settings_dlg->get_housing_settings(m_hs);
    if(!mp_dbms->open_db(m_hs)) return;
    ui->qact_connect->setChecked(true);
    ui->qact_connect->setText(RUSTR("Подключено"));
    QFont font = ui->qact_connect->font();
    font.setBold(true);
    ui->qact_connect->setFont(font);

   // mp_get_receipt->load_list();
    mp_develop->db_ready();

    ui->qact_town->setEnabled(true);
    ui->qact_tenants->setEnabled(true);
    ui->qact_leases->setEnabled(true);
    ui->qact_receipt->setEnabled(true);
    ui->qact_decmasters->setEnabled(true);
    ui->qact_payments->setEnabled(true);
    ui->qact_reports->setEnabled(true);

    mp_town_editor->update_nav_model();
    mp_people_editor->update_models();
    mp_decmaster_editor->update_models();
    mp_leases_editor->update_model();
    mp_payments->update_data();
}

void LKHWindow::on_qact_show_develop_area_activated()
{
        show_work_widget(mp_develop, "develop area");
}

void LKHWindow::on_qact_about_activated()
{
    show_work_widget(mp_about_dlg, RUSTR("О программе!"));

}


void LKHWindow::get_msg(QString text)
{
    text = RUSTR("(№") + QString::number(ui->msgs_list->count()+1)+")\t"+text;
    ui->msgs_list->addItem(text);
    ui->msgs_list->scrollToBottom();
}

void LKHWindow::get_error_msg(QString text)
{
    qDebug()<<text;
    text = RUSTR("(№") + QString::number(ui->msgs_list->count()+1)+")\t"+text;
    QListWidgetItem *p_i = new QListWidgetItem(text);
    p_i->setTextColor(QColor(255,0,0));
    ui->msgs_list->addItem(p_i);
    ui->msgs_list->scrollToBottom();

    if(m_hs.str_home_path.size() == 0) return;
    QString qstr_file_name = m_hs.str_home_path + "/lakdb-errors.log";
    QFile qf_err(qstr_file_name);
    qf_err.open(QIODevice::WriteOnly | QIODevice::Append);
    if(!qf_err.isOpen()) return;
    QTextStream qts(&qf_err);
    qts<<QDateTime::currentDateTime().toString("hh:mm:ss - dd.MM.yyyy")<<": "<<text<<"\n";
    qf_err.close();
}


void LKHWindow::get_fast_msg(QString text)
{
    ui->infoBar->showMessage(text);
}

void LKHWindow::on_qact_receipt_activated()
{
    show_work_widget(mp_get_receipt, RUSTR("Экспорт квитанций."));
}

void LKHWindow::on_qact_town_activated(int )
{
    show_work_widget(mp_town_editor, RUSTR(" town editor"));
}

void LKHWindow::on_qact_tenants_activated(int )
{
     show_work_widget(mp_people_editor, RUSTR("people editor"));
}

void LKHWindow::on_qact_leases_activated(int )
{
    show_work_widget(mp_leases_editor, RUSTR("leases"));
}

void LKHWindow::on_qact_decmasters_activated(int )
{
    show_work_widget(mp_decmaster_editor, RUSTR("decision masters editor"));
}

void LKHWindow::on_qact_payments_activated(int )
{
    show_work_widget(mp_payments, QString());
}

void LKHWindow::on_qact_reports_activated()
{
    show_work_widget(mp_reports, QString());
}
